//
//  WLLUserCountModel.swift
//  WLLWeibo
//
//  Created by 王乐乐 on 16/8/10.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
//typealias lele = String
class WLLUserAcountModel: NSObject,NSCoding{
        /// 用户授权的唯一票据
    var access_token : String?
        /// access_token的生命周期，单位是秒数。---interval 间隔
    var expires_in : NSTimeInterval = 0{
        didSet {
            expires_date = NSDate(timeIntervalSinceNow: expires_in)
        }
    }
        /// 授权用户的UID
    var uid : String?
        /// 用户昵称
    var screen_name :String?
        /// 用户头像地址（大图），180×180像素
    var avatar_large :String?
        /// 过期日期(程序猿自己加的字段)
    var expires_date :NSDate?
    //TODO:归档到哪去?
     //MARK: - 归档 archiver
    func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encodeObject(access_token, forKey: "access_token")
        aCoder.encodeObject(uid, forKey: "uid")
        aCoder.encodeObject(screen_name, forKey: "screen_name")
        aCoder.encodeObject(avatar_large, forKey: "avatar_large")
        aCoder.encodeObject(expires_date, forKey: "expires_date")
        
    }
    
     //MARK: - 解档  遵循NSCoding协议
    required init?(coder aDecoder: NSCoder) {
        access_token = aDecoder.decodeObjectForKey("access_token")as?String
        uid = aDecoder.decodeObjectForKey("uid")as?String
        screen_name = aDecoder.decodeObjectForKey("screen_name")as?String
        avatar_large = aDecoder.decodeObjectForKey("avatar_large")as?String
        expires_date = aDecoder.decodeObjectForKey("expires_date")as?NSDate
        
    }
    
    
    //方法的重载
    init(dict:[String:AnyObject]) {
        super.init()
        
         setValuesForKeysWithDictionary(dict)
        
    }
    //WARNING:SSSS
    //FIXME:未来记得修改我
    //TODO:等待将来回来做
    //防止 key 的报错
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
        
    }
    
    /**
     *  重写 description
     */
    override var description: String{
        //传入的 dict 里没有"expires_date"的值,但是,由于有 setValue...forUnDefinedKey 防报错.所以没有报错
        //因为外界给expires_in赋值, didSet 被触发,然后expires_date被赋值...
        let keys = ["access_token","expires_date","uid","screen_name","avatar_large"]
        //这里返回的是一个字典
        return dictionaryWithValuesForKeys(keys).description
    }
    
}
